package com.zt.rongda.sign.controller;

import com.zt.servlet.*;
import com.zt.rongda.sign.model.Sign;
import com.zt.rongda.sign.service.SignService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/sign")
public class SignController {

    @Autowired
    private SignService signService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(signService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        signService.add(getSign(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        signService.update(getSign(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        signService.delete(reqBean.getInfo("signId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Sign getSign(ReqBean reqBean) {
        Sign sign = new Sign();
        sign.setSignId(reqBean.getInfo("signId"));
        sign.setLocation(reqBean.getInfo("location"));
        sign.setEmpId(reqBean.getInfo("empId"));
        sign.setSignTime(reqBean.getInfoDate("signTime"));
        sign.setLongitude(reqBean.getInfo("longitude"));
        sign.setLatitude(reqBean.getInfo("latitude"));
        sign.setOperator(reqBean.getUserId());
        sign.setRemark(reqBean.getInfo("remark"));
        return sign;
    }

}